package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (C), 2021-2022
 * Author: 16372
 * Date: 2022/2/26 15:14
 * FileName: RoleController
 * Description: 角色管理
 */
@RestController
@RequestMapping("sys_role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * @Author 16372
     * @Description //TODO 角色分页列表模糊查询
     * @Date 15:40 2022/2/26
     * @Param [page, limit, name]
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/list")
    public ResultData getList(Integer page,Integer limit,String name){
        Page<Role> rolePage = new Page<>(page,limit);
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null,Role::getName,name);
        roleService.page(rolePage,lambdaQueryWrapper);
        return ResultData.ok("", PageResult.init(rolePage));
    }

    /**
     * @Author 16372
     * @Description //TODO 根据id查询角色详情
     * @Date 15:44 2022/2/26
     * @Param [id]
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        Role role = roleService.getById(id);
        return ResultData.ok("",role);
    }
    
    /**
     * @Author 16372
     * @Description //TODO 角色修改
     * @Date 15:46 2022/2/26
     * @Param [role]
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/update")
    public ResultData update(@RequestBody Role role){
        roleService.updateById(role);
        return ResultData.ok("","");
    }
    
}
